package com.shitou.shitougateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;


/**
 * @auto 石头人
 * 跨域配置
 */
@SuppressWarnings("all")
@Configuration
public class GulimallCorsConfig {

    @Bean
    public CorsWebFilter corsWebFilter(){
        UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
        CorsConfiguration corsConfiguration = new CorsConfiguration();

        corsConfiguration.addAllowedHeader("*");  // 允许哪些请求头进行跨域
        corsConfiguration.addAllowedMethod("*");  // 允许哪些请求方式跨域
        corsConfiguration.addAllowedOrigin("*");  // 允许哪些请求来源进行跨域
        corsConfiguration.setAllowCredentials(true);   // 是否允许携带cookie进行跨域

        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**",corsConfiguration);

        return new CorsWebFilter(urlBasedCorsConfigurationSource);
    }
}
